package com.infinitus.autocatlog.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infinitus.autocatlog.service.ConfigInfoService;


@Service
public class ConfigInfoServiceImpl implements ConfigInfoService {

	@Autowired
	private EntityManagerFactory entityManagerFactory;
//	@PersistenceContext
//	private EntityManager entityManager;
	


	@Override
	public Map<String, String> getTopicRules() {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		List<Object[]> res = entityManager.createNativeQuery("SELECT sys_name, topic from topic_rules ").getResultList();
		Map<String, String> map = new HashMap<String, String>();
		for(Object[] arr: res){
			map.put(((String)arr[0]).toUpperCase(), (String)arr[1]);
		}
		entityManager.clear();
		entityManager.close();
		return map;
	}
	
	@Override
	public Map<String,Set<String>> getTypeFromPage() {
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		List<Object[]> res = entityManager.createNativeQuery("SELECT type, page, action, label from page_value_type ").getResultList();
		Map<String,Set<String>> map = new HashMap<String,Set<String>>();
		for(Object[] arr: res){
			Set<String> set = map.get( ((String) arr[0]));
			if(set==null){
				set = new HashSet<String>();
			}
			set.add(arr[1]+"_"+arr[2]+"_"+arr[3]);
			map.put(((String) arr[0]), set);
		}
		entityManager.clear();
		entityManager.close();
		return map;
	}
	
}
